package com.java.dao.Impl;

import com.java.dao.OrderDao;
import com.java.entity.Orders;
import com.java.util.C3P0Util;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());

    @Override
    public List<Orders> selectOrderById(String Id) throws SQLException {
        String sql="select * from f_order where Id=?";
        List<Orders> orders= qr.query(sql, new ResultSetHandler<List<Orders>>() {

            @Override
            public List<Orders> handle(ResultSet rs) throws SQLException {
                Orders orders=null;
                List<Orders> list=new ArrayList<>();
                while (rs.next()){
                    orders=new Orders();
                    orders.setBookId(rs.getString("bookId"));
                    orders.setName(rs.getString("name"));
                    orders.setFlightNo(rs.getString("flightNo"));
                    orders.setAirlineNo(rs.getString("airlineNo"));
                    orders.setOrigin(rs.getString("origin"));
                    orders.setDestination(rs.getString("destination"));
                    orders.setLeaveTime(rs.getString("leaveTime"));
                    orders.setArriveTime(rs.getString("arriveTime"));
                    orders.setTicketPrice(rs.getInt("ticketPrice"));
                    orders.setPaydastatus(rs.getInt("paydastatus"));
                    orders.setLeave(rs.getString("leave"));
                    orders.setArrive(rs.getString("arrive"));
                    list.add(orders);

                }
                return list;
            }
        },Id);


        return orders;
    }

    /**
     * 根据订单号查询订单相关信息
     * @param bookId
     * @return
     * @throws SQLException
     */
    @Override
    public Orders selectOrderByBookId(String bookId) throws SQLException {
        Orders order = null;

        String sql="select * from f_order where bookId=?";
        order = qr.query(sql, new ResultSetHandler<Orders>() {
            @Override
            public Orders handle(ResultSet rs) throws SQLException {
                Orders orders=new Orders();
                while (rs.next()){
                    orders.setBookId(rs.getString("bookId"));
                    orders.setFlightNo(rs.getString("flightNo"));
                    orders.setAirlineNo(rs.getString("airlineNo"));
                    orders.setOrigin(rs.getString("origin"));
                    orders.setDestination(rs.getString("destination"));
                    orders.setLeaveTime(rs.getString("leaveTime"));
                    orders.setArriveTime(rs.getString("arriveTime"));
                    orders.setName(rs.getString("name"));
                    orders.setTicketPrice(rs.getInt("ticketPrice"));
                    orders.setPhone(rs.getString("phone"));
                    orders.setLeave(rs.getString("leave"));
                    orders.setArrive(rs.getString("arrive"));

                }

                return orders;
            }
        },bookId);


        return order;
    }


}
